//
//  KABRCIMChatVC.m
//  kabu
//
//  Created by XiangDongMBP on 2017/4/19.
//  Copyright © 2017年 East. All rights reserved.
//

#import "KABRCIMChatVC.h"
#import "MBProgressHUD+XDPrompt.h"
#import "KABModel.h"
#import "GlobalHeader.h"
#import "ApiManager.h"
#import "XDAFNetworking.h"
#import "KABMallMyShopVC.h"

@interface KABRCIMChatVC ()
@property (nonatomic, strong) UIBarButtonItem *shopBarBtnItem;
@property (nonatomic, strong) KABMineIMUserInfo *userInfo;
@end

@implementation KABRCIMChatVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initNavigationBar];
    [self initSubViews];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initNavigationBar {
//    self.navigationItem.rightBarButtonItem = self.shopBarBtnItem;
}

- (void)initSubViews {
    // 去掉定位
    NSArray *array = self.chatSessionInputBarControl.pluginBoardView.allItems;
    [self.chatSessionInputBarControl.pluginBoardView removeItemAtIndex:array.count - 1];
    // 去掉名称显示
    self.displayUserNameInCell = NO;
    
    // 请求用户信息
    [self requestIMUserInfo];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - delegate
- (void)didTapCellPortrait:(NSString *)userId {
    ;
}

#pragma mark - event response
- (void)shopBarBtnTapped:(UIBarButtonItem *)item {
    KABMallMyShopVC *vc = [[KABMallMyShopVC alloc] init];
    vc.shop_owner_id = self.userInfo.user_id;
    [self showViewController:vc sender:nil];
}

#pragma mark - private methods
- (void)refreshWholeView {
    if (self.userInfo.is_merchant) {
        self.navigationItem.rightBarButtonItem = self.shopBarBtnItem;
    }
}

#pragma mark - network request
- (void)requestIMUserInfo {
    if (!self.targetId.length) {
        return;
    }
    
    MBProgressHUD *hud = [MBProgressHUD xd_showHUDAddedTo:self.view animated:YES];
    
    NSDictionary *pars = @{@"user_id" : self.targetId};
    
    [[XDAFNetworking sharedInstance] GET:[ApiManager apiMineUserSimpleInfo] parameters:pars progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"请求IM用户详情返回：%@", responseObject);
        [hud hideAnimated:YES];
        if ([responseObject[KABGlobalJsonKeyErrCode] integerValue] == 200) {
            self.userInfo = [KABMineIMUserInfo yy_modelWithDictionary:responseObject[KABGlobalJsonKeyResult]];
            [self refreshWholeView];
        } else {
            [MBProgressHUD showPromptWithAutoHideInView:self.view message:responseObject[KABGlobalJsonKeyMsg]];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求IM失败：%@", error);
        [hud hideAnimated:NO];
//        [MBProgressHUD showPromptWithAutoHideInView:self.view message:@"网络请求失败"];
    }];
}

#pragma mark - getters and setters
- (UIBarButtonItem *)shopBarBtnItem {
    if (!_shopBarBtnItem) {
        _shopBarBtnItem = [[UIBarButtonItem alloc] initWithTitle:@"进店" style:UIBarButtonItemStylePlain target:self action:@selector(shopBarBtnTapped:)];
    }
    return _shopBarBtnItem;
}

@end
